<?php
/**
 * Created by Jayli.
 * User: Administrator
 * Date: 2018-03-07
 * Time: 14:46
 */

namespace App\Http\Controllers\Web\Member\Seller;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class OrdersController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'seller/orders'],
        'show' => ['get', 'seller/orders/%d'],
        'store' => ['post', 'seller/orders'],
        'update' => ['put', 'seller/orders/%d'],
        'delete' => ['delete', ''],
        'index_express_company' => ['get', 'seller/express_company'],
        'company_show' => ['get', 'shop/company_show/%d'],

    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        // ini_set('max_execution_time', '0');
        //参数部分
        $search_params = $this->request->all(['order_code', 'created_at','order_status','keyword']);
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page,
            'with' => 'orderProducts;shop;items_products;items_information',
            'orderBy'=>'id',
            'sortedBy'=>'desc',
        ];

        //请求部分
        $ret = http_web($this->api_url['index'])->send($params);

        //结果处理部分
        $data = paginate($this->request, $ret['data']);
        foreach($data['list'] as $k=>$v){
            if(!empty($v['items_information'])){
                $data['list'][$k]['items_information']['quote_contain']=\GuzzleHttp\json_decode($v['items_information']['quote_contain'],true);
            }
        }
        //请求部分-快递公司
        $express_company = http_web($this->api_url['index_express_company'])->send(['type' => 'kv']);
        $data['express_company'] = $express_company['data'];

        //回填搜索框值
        $data['search_params'] = $search_params;

        return view('web.user.seller.order_list', $data);
    }

    public function show($id)
    {
        //参数部分
        $params = [
            'with' => 'shop;orderProducts;invoice;user;orderExpress;userContracts;items_products;items_information'
        ];
        //请求部分
        $ret = http_web($this->api_url['show'], [$id])->send($params);

        if(isset($ret['data']['shop']['sp_cmp_id'])){
            $company_id=$ret['data']['shop']['sp_cmp_id'];
            $company = http_web($this->api_url['company_show'], [$company_id])->send();
            $ret['data']['company']=$company['data'];
        }else{
            $ret['data']['company']=[];
        }
        if(!empty($ret['data']['items_information'])){
            $ret['data']['items_information']['quote_contain']=\GuzzleHttp\json_decode($ret['data']['items_information']['quote_contain'],true);
        }
        //请求部分-快递公司
        $express_company = http_web($this->api_url['index_express_company'])->send(['type' => 'kv']);

        return view('web.user.seller.order_details', ['order' => $ret['data'], 'express_company' => $express_company['data']]);
    }

    public function edit($id)
    {
        return view('web.user.seller.common.order_cancel', ['order_id' => $id]);
    }

    public function update($id)
    {
        $this->validate($this->request, [
            'order_status' => 'required|in:4,5',
            'cancel_reason' => 'integer'
        ]);
        $data = $this->request->all(['order_status', 'cancel_reason']);

        //请求部分
        $ret = http_web($this->api_url['update'], [$id])->send($data);
        return $ret;
    }
}